<template>
  <div >
    <div v-if="mixedLineId === ''">
      <div class="my-code">点击混线配置查看详情</div>
    </div>
    <!--工具栏-->
    <div class="head-container" v-if="mixedLineId != ''">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="550px">
        <el-form :inline="true"  ref="form" :model="form" :rules="rules" size="small" label-width="100px">
<!--          <el-form-item label="产品类型" prop="productType">
            <el-input v-model="form.productType" style="width: 370px;" />
          </el-form-item>-->
          <el-form-item label="产品型号" prop="productCode">
            <el-select @change="productCodeChange" v-model="form.productCode" style="width: 370px" filterable placeholder="请选择">
              <el-option
                v-for="item in products"
                :key="item.id"
                :label="item.modelCode"
                :value="item.modelCode" >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="优先级别" prop="priorityLevel">
            <el-input-number v-model="form.priorityLevel" :step="1" step-strictly></el-input-number>
          </el-form-item>
          <el-form-item label="循环数量" prop="cycleQty">
            <el-input-number v-model="form.cycleQty" :step="1" step-strictly></el-input-number>
          </el-form-item>

          <el-form-item label="已循环数量" prop="processQty">
            <el-input-number v-model="form.processQty" :step="1" step-strictly></el-input-number>
          </el-form-item>

          <el-form-item label="是否在执行" prop="inProcess">
            <el-radio-group v-model="form.inProcess">
              <el-radio :label="item.value" v-for="item in dict.whether">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="描述">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="productCode" label="产品型号" width="200px;" align="center"/>
        <el-table-column prop="productType" label="产品类型" width="100px;" align="center"/>
        <el-table-column prop="cycleQty" label="生产数量" width="100px;" align="center"/>
        <el-table-column prop="priorityLevel" label="优先级别" width="100px;" align="center" />
        <el-table-column prop="inProcess" label="正在执行" width="100px;" align="center">
          <template slot-scope="scope">
            {{ dict.label.whether[scope.row.inProcess] }}
          </template>
        </el-table-column>
        <el-table-column prop="processQty" label="已执行数量" width="100px;" align="center"/>
        <el-table-column prop="remark" label="描述" width="200px;" align="center"/>
        <el-table-column prop="createBy" label="创建人" width="100px;" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="150px;" align="center"/>
        <el-table-column prop="updateBy" label="更新人" width="100px;" align="center"/>
        <el-table-column prop="updateTime" label="更新时间" width="150px;" align="center"/>
        <el-table-column v-if="checkPer(['admin','planMixedRule:edit','planMixedRule:del'])" fixed="right" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPlanMixedRule from '@/api/plan/planMixedRule'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

import crudProductModelLien from "@/api/product/productModelLien";
import crudProductModel from "@/api/product/productModel";

const defaultForm = { id: null, mixedLineId: null, productType: null, productCode: null, cycleQty: 0, priorityLevel: 0, inProcess: '0', processQty: 0, remark: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'PlanMixedRule',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['whether'],
  cruds() {
    return CRUD({
      title: '混线规则',
      url: 'api/planMixedRule',
      idField: 'id',
      sort: 'id,desc',
      queryOnPresenterCreated: false,
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false,
        reset: false,
        import: false
      },
      crudMethod: { ...crudPlanMixedRule }})
  },
  data() {
    return {
      mixedLineId: '',
      lineCode: '',
      sectionCode: '',
      products: [],
      permission: {
        add: ['admin', 'planMixedRule:add'],
        edit: ['admin', 'planMixedRule:edit'],
        del: ['admin', 'planMixedRule:del']
      },
      rules: {
        mixedLineId: [
          { required: true, message: '混线ID不能为空', trigger: 'blur' }
        ],
        productType: [
          { required: true, message: '产品类型不能为空', trigger: 'blur' }
        ],
        productCode: [
          { required: true, message: '产品型号不能为空', trigger: 'blur' }
        ],
        cycleQty: [
          { required: true, message: '生产数量不能为空', trigger: 'blur' }
        ],
        priorityLevel: [
          { required: true, message: '优先级别不能为空', trigger: 'blur' }
        ],
        inProcess: [
          { required: true, message: '正在执行不能为空', trigger: 'blur' }
        ],
        processQty: [
          { required: true, message: '已执行数量不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'mixedLineId', display_name: '混线ID' },
        { key: 'productType', display_name: '产品类型' },
        { key: 'productCode', display_name: '产品型号' },
        { key: 'cycleQty', display_name: '生产数量' },
        { key: 'priorityLevel', display_name: '优先级别' },
        { key: 'inProcess', display_name: '正在执行' },
        { key: 'processQty', display_name: '已执行数量' },
        { key: 'remark', display_name: '描述' },
        { key: 'createBy', display_name: '创建人' },
        { key: 'updateBy', display_name: '更新人' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.params.mixedLineId = this.mixedLineId;
      return true
    },[CRUD.HOOK.beforeToAdd](crud) {
      crud.form.mixedLineId = this.mixedLineId;
      return true
    },
    getProduct(){
      crudProductModelLien.list({lineCode: this.lineCode,sectionCode: this.sectionCode}).then(res =>{
        this.products = res;
      })
    },
    productCodeChange(value){
      debugger
      crudProductModel.list({productCode : value}).then(res =>{
        debugger
        if(res && res.length > 0){
          this.crud.form.productType = res[0].productType;
        } else{
          this.crud.form.productType = '';
        }
      });
    },
    //加载页面
    queryPage(id,lineCode,sectionCode){
      this.mixedLineId = id;
      this.lineCode = lineCode;
      this.sectionCode = sectionCode;
      this.crud.toQuery();
      this.getProduct();
    }
  }
}
</script>

<style scoped>

</style>
